<?php
namespace Admin\Controller;
use Think\Controller;
class RuleController extends CommonController {
	// 展示
    public function indexAction(){
    	//统计规则数
    	$sql = "select count(id) count from auth_rule";
    	$count = M()->query($sql);
    	$count = current(M()->query($sql))['count'];

    	// 分页
    	$Page  = new \Think\Page($count,5);// 实例化分页类 传入总记录数和每页显示的记录数(25)
        $show  = $Page->show();// 分页显示输出

        // 查询规则分类信息
        $sql = "select t1.*,t2.cname from auth_rule t1 left join auth_rule_category t2 on t1.cid=t2.id limit {$Page->firstRow},{$Page->listRows}";
        $res = M()->query($sql);

        $sql = "select * from auth_rule_category";

        $categoryData = M()->query($sql);
        // var_dump($res);exit;

        $this->assign('count',$count);
        $this->assign('categoryData',$categoryData);
        $this->assign('show',$show);
    	$this->assign('res',$res);
    	$this->display();
    }

    // 添加
    public function addAction(){
    	if (IS_POST) {
    		var_dump($_POST);
    		$title = I('post.title','');
            $name = I('post.name','');
            $cid = I('post.cid','');
            $sql = "insert into auth_rule (name,title,cid) value('{$name}','{$title}','{$cid}')";
            $res = M()->execute($sql);
            $this->assign('res',$res);
            // var_dump($res);
            if ($res) {
            	$this->success('添加成功',U('admin/rule/index'));
            }else{
            	$this->error('添加失败',U('admin/rule/index'));
            }
    	}
    }

    // 修改
    //如果有post数据，那么接到对应数据取修改，并显示修改成功或修改失败。如果没有post数据，那么就get数据，如果get的id是空的，那么就是非法请求，如果非空，那么就查询老数据，展示老数据
    public function editAction(){
    	if (IS_POST) {
    		var_dump($_POST);
    		$title = I('post.title','');
            $name = I('post.name','');
            $id = I('post.id','');
            $cid = I('post.cates','');
            $sql = "update auth_rule set title='{$title}',name='{$name}',cid='{$cates}' where id='{$id}'";
            $res = M()->execute($sql);
            if ($res) {
            	$this->success('修改成功',U('admin/rule/index'));
            }else{
            	$this->success('修改失败',U('admin/rule/index'));
            }
    	}else{
    		$id = I('get.id','');
    		if (empty($id)) {
    			$this->error('非法请求',U('admin/rule/index'));
    		}else{
    			$dropdownListSql = "select * from auth_rule_category";
    			$dropdownListRes = M()->query($dropdownListSql);
    			$sql = "select t1.*,t2.cname from auth_rule t1 left join auth_rule_category t2 on t1.cid = t2.id where t1.id = '{$id}'";
    			$res = M()->query($sql);
    			$this->assign('res',current($res));
    			$this->assign('dropdownListRes',$dropdownListRes);
    			$this->display();
    		}
    	}        	
    }

    //删除
    public function delAction(){
    	$id = I('get.id');

        $sql = "delete from  auth_rule where id='{$id}'";

        $res = M()->execute($sql);

        if($res){
            $this->success('删除成功',U('admin/rule/index'));
        }else{
            $this->error('删除失败',U('admin/rule/index'));
        }
    }

    

}